package cn.aitrox.ry.service.user.service.impl;

import cn.aitrox.ry.common.bean.Order;
import cn.aitrox.ry.service.user.dao.CompanyDao;
import cn.aitrox.ry.service.user.ennumtype.CompanyStatusEnum;
import cn.aitrox.ry.service.user.entity.CompanyEntity;
import cn.aitrox.ry.service.user.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDao companyDao;

    @Override
    public CompanyEntity findById(Integer id) {
        return companyDao.findById(id);
    }

    @Transactional
    @Override
    public void create(String name, String remark) {
        CompanyEntity companyEntity = new CompanyEntity(null, name, remark, CompanyStatusEnum.ENABLE.getStatus());
        companyDao.insertSelective(companyEntity);
    }

    @Transactional
    @Override
    public void edit(Integer id, String name, String remark, Integer status) {
        CompanyEntity update = new CompanyEntity(id, name, remark, status);
        companyDao.updateByIdSelective(update);
    }

    @Override
    public List<CompanyEntity> findAll() {
        return companyDao.findAllOrderBy(Order.desc("id"));
    }

    @Override
    public List<CompanyEntity> findByIds(List<Integer> ids) {
        return companyDao.findByIds(ids.toArray(), null);
    }

    @Override
    public CompanyEntity findByName(String name) {
        if (null == name) {
            return null;
        }
        CompanyEntity cond = new CompanyEntity();
        cond.setName(name);
        return companyDao.findOne(cond);
    }
}
